﻿using System;
using System.Windows.Forms;
using CosyTrade.Data;
using CosyTrade.Financial;
using CosyTrade.UI;

namespace CosyTrade.ClientForm.Financial
{
    public partial class ExchangeRateDetailForm : DetailFormBase
    {
        public ExchangeRateDetailForm():this(Guid.Empty){}

        public ExchangeRateDetailForm(Guid objKey) : base(objKey)
        {
            InitializeComponent();
        }

        protected override BusinessObject CreateNewBusinessObject()
        {
            return new ExchangeRate();
        }

        protected override BusinessObject InitBusinessObject(Guid currObjKey)
        {
            return Operation.FinancialSystem.GetExchangeRateDetail(currObjKey);
        }

        protected override string GetTitle()
        {
            return "新建汇率";
        }

        protected override void DataBinding()
        {
            BindingControl(txtIndex, "EditValue", "ExchangeIndex");
            BindingControl(dtLogDate, "EditValue", "SetDate");
            BindingControl(txtExchange, "EditValue", "Exchange");
            BindingControl(txtMemo, "EditValue", "Memo");
            BindingControl(cbDefaultRate, "EditValue", "CurrentRate");
        }

        protected override bool IsVail()
        {
            string msg = string.Format("是否创建汇率为 1美元兑换 {0}委内瑞拉币的汇率记录？", ((ExchangeRate) CurrentObject).Exchange);
            if (Functions.ShowConfigBox(msg) != DialogResult.Yes) return false;
            return true;
        }

        protected override void Save(BusinessObject currentObj)
        {
            if (!(currentObj is ExchangeRate)) return;

            ((ExchangeRate) currentObj).Company = GlobalInfo.CurrentCompany;

            switch (CurrentStat)
            {
                case DetailFormCurrentStat.AddStat:
                    Operation.FinancialSystem.AddExchangeRate((ExchangeRate) currentObj, cbDefaultRate.Checked);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        protected override void AfterSave()
        {
            Close();
        }
    }
}
